#include "../mqserver/exchange.hpp"
#include <gtest/gtest.h>

mmq::ExchangeManager::ptr emp;

class ExchangeTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        emp = std::make_shared<mmq::ExchangeManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        // emp->Clear();
    }
};

TEST(exchange_test, insert_test)
{
    // std::unordered_map<std::string, std::string> map = {{"k1", "v1"}, {"k2", "v2"}};
    std::unordered_map<std::string, std::string> map;
    emp->DeclareExchange("exchange1", mmq::ExchangeType::DIRECT, true, false, map);
    emp->DeclareExchange("exchange2", mmq::ExchangeType::DIRECT, true, false, map);
    emp->DeclareExchange("exchange3", mmq::ExchangeType::DIRECT, true, false, map);
    emp->DeclareExchange("exchange4", mmq::ExchangeType::DIRECT, true, false, map);
    ASSERT_EQ(emp->Size(), 4); 
}

TEST(exchange_test, select_test)
{
    ASSERT_EQ(emp->Exists("exchange1"), false);
    ASSERT_EQ(emp->Exists("exchange2"), true);
    ASSERT_EQ(emp->Exists("exchange3"), true);
    ASSERT_EQ(emp->Exists("exchange4"), true);
    mmq::Exchange::ptr exp = emp->SelectExchange("exchange2");
    ASSERT_NE(exp.get(), nullptr);
    ASSERT_EQ(exp->_name, "exchange2");
    ASSERT_EQ(exp->_type, mmq::ExchangeType::DIRECT);
    ASSERT_EQ(exp->_durable, true);
    ASSERT_EQ(exp->_auto_delete, false);
    ASSERT_EQ(exp->GetArgs(), std::string("k1=v1&k2=v2&"));
}

TEST(exchange_test, remove_test)
{
    emp->DeleteExchange("exchange2");
    mmq::Exchange::ptr exp = emp->SelectExchange("exchange2");
    ASSERT_EQ(exp.get(), nullptr);
    ASSERT_EQ(emp->Exists("exchange2"), false);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    int ret = RUN_ALL_TESTS();

    return 0;
}